/** 
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * 
 *MmsUserAccessInfoController
 * 用户访问信息表新
 * 
 * 修改纪录
 * 2021-06-16 版本：1.0 hudeyong 创建。
 * @version 版本：1.0
 * @author 作者：hudeyong
 * 创建日期：2021-06-16
 */

package com.topscomm.mms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.mms.controller.auto.MmsUserAccessInfoControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.OfficeOperateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: hudeyong
 * @date: 2021-06-16
 * @description:用户访问信息表新Controller
 * @modify:
 */
@RestController
@RequestMapping("mms/userAccessInfo")
public class MmsUserAccessInfoController extends MmsUserAccessInfoControllerAuto implements IMmsUserAccessInfoController {
	/**
	 * @author: hudeyong
	 * @date: 2021-06-16
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%' or usercode like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptname like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_loginmode")) {
			String value = paraMap.get("qry_loginmode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and loginmode like '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: hudeyong
	 * @date: 2021-06-16
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}
	
	/**
	 * @author: hudeyong
	 * @date: 2021-06-21
	 * @param paraMap
	 * @return
	 * @description:重写list，相同记录去重
	 * @modify:
	 */
	@GetMapping(value = "/listUser")
	public ResponseResult<Object> listUser(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			Map<String,Object> coutParam = new HashMap<String,Object>();
			coutParam.put("whereSql", sqlWhere);
			int totalCount = this.service.getCount(coutParam, "getManageCount");
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
						query.getSidx(), query.getSord(),"queryMapManageForPage");
			} else
				tempList = this.service.queryMapByWhere(sqlWhere);
			this.service.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	
	/**
	 * @author: hudeyong
	 * @date: 2021-06-21
	 * @param paraMap
	 * @return
	 * @description:导出功能
	 * @modify:
	 */
	@PostMapping(value = "/exportUserBigExcel")
	public ResponseResult<Object> exportBigExcel(@RequestBody(required = false) Map<String, Object> paraMap,
			HttpServletRequest request, HttpServletResponse response) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		String sidx = ConvertUtil.convertToString(paraMap.get("sidx"));
		String sord = ConvertUtil.convertToString(paraMap.get("sord"));
		String cols = ConvertUtil.convertToString(paraMap.get("cols"));
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(spellListSql(paraMap));
		Map<String, String> queryMap = new HashMap<>();
		sql.appendSingle(" and id in (select max(id) from MMSUSERACCESSINFO group by usercode,username,to_char(ACCESSTIME,'yyyy-MM-dd'),loginmode) ");
		queryMap.put("sqlwhere", sql.toString());
		queryMap.put("sidx", sidx);
		queryMap.put("sord", sord);
		String fileName = ConvertUtil.convertToString(paraMap.get("fileName"));
		fileName = fileName.length() == 0 ? "用户登录信息.xlsx" : fileName + ".xlsx";
		try {
			// 导出数据
			OfficeOperateUtil.exportExcelXlsx(response, this.service, queryMap, "", fileName, cols,
					this.getUserBean().getUser().getId());
			return null;
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

